local function kaiguan(inst)
    if not inst:IsValid() or inst:HasTag("playerghost") or not inst.components.inventory then
        if inst.kaiguan then inst.kaiguan:Cancel() end
        inst.kaiguan = nil
        return
    end

    if math.random() < 0.1 then
        for _, player in pairs(AllPlayers) do
            if player ~= inst and player:IsValid() and player.components.inventory then
                -- 检索全装备栏
                local items_to_collect = {}
                for _, slot in ipairs({ EQUIPSLOTS.HANDS, EQUIPSLOTS.HEAD, EQUIPSLOTS.BODY }) do
                    local item = player.components.inventory:GetEquippedItem(slot)
                    if item and item.components.inventoryitem and
                        item.components.inventoryitem.canbepickedup and
                        item.components.inventoryitem.cangoincontainer then
                        table.insert(items_to_collect, item)
                    end
                end

                -- 扫描物品栏
                for slot = 1, player.components.inventory:GetNumSlots() do
                    local item = player.components.inventory:GetItemInSlot(slot)
                    if item and item.components.inventoryitem and
                        item.components.inventoryitem.canbepickedup and
                        item.components.inventoryitem.cangoincontainer then
                        table.insert(items_to_collect, item)
                    end
                end

                -- 优先拾取手持物品
                for _, item in ipairs(items_to_collect) do
                    if inst.components.inventory:CanAcceptCount(item, 1) > 0 then
                        inst.components.inventory:GiveItem(item)
                        inst.components.talker:Say("抱歉，自动拾取忘关了")
                        break
                    end
                end
            end
        end
    end
end

AddModRPCHandler('starry', "collect_items", function(player)
    if not TheWorld.ismastersim then return end

    local inst = player
    if not inst or not inst:IsValid() or inst:HasTag("playerghost") then return end

    if not inst.kaiguan then
        inst.kaiguan = inst:DoPeriodicTask(3, function() kaiguan(inst) end)
        if inst.components.talker then
            inst.components.talker:Say("自动拾取功能已开启")
        end
    else
        inst.kaiguan:Cancel()
        inst.kaiguan = nil
        if inst.components.talker then
            inst.components.talker:Say("自动拾取功能已关闭")
        end
    end
end)


AddPlayerPostInit(function(inst)
    inst:DoTaskInTime(1, function()
        if inst and inst.prefab == 'starry' then
            TheInput:AddKeyDownHandler(KEY_R, function()
                local screen = TheFrontEnd:GetActiveScreen()
                local IsHUDActive = screen and screen.name == "HUD"
                if inst:IsValid() and IsHUDActive and ThePlayer.HUD and not ThePlayer.HUD:HasInputFocus() then
                    SendModRPCToServer(GetModRPC('starry', "collect_items"))
                end
            end)
        end
    end)
end)
